package com.gjzt.business.bid.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.DateUtil;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.bid.inter.BiddingDepositMapper;
import com.gjzt.business.bid.inter.BiddingMapper;
import com.gjzt.business.bid.model.Bidding;
import com.gjzt.business.bid.model.BiddingDeposit;
import com.gjzt.business.customer.inter.CustomerMapper;
import com.gjzt.business.customer.model.Customer;
import com.gjzt.business.utils.Constant;
import com.unionpay.acp.sdk.LogUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class BiddingDepositService extends BaseService {

    @Autowired
    private BiddingDepositMapper biddingDepositMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BiddingMapper biddingMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findBiddingDeposit(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("SELECT abd.*, ac.CUSTOMER_NAME,ab.BID_TITLE FROM AD_BIDDING_DEPOSIT abd");
        sqlBuilder.appendSql(" LEFT JOIN AD_CUSTOMER ac ON abd.CUSTOMER_ID = ac.CUSTOMER_ID ");
        sqlBuilder.appendSql(" LEFT JOIN AD_BIDDING ab ON ab.BID_ID = abd.BID_ID where 1=1 ");
        sqlBuilder.appendSql(" and ab.BID_ID = "+reqBean.getInfo("bidId"));
        if (Constant.QUICK_SEARCH_FALSE.equals(reqBean.getInfo("quickSearch"))) {// 正常查询
        	 sqlBuilder.addWhereLike2(" ab.BID_TITLE", reqBean.getInfo("bidTitle"))
			  .addWhere(" abd.DEPOSIT_SOURCE", reqBean.getInfo("depositSource"))
			  .addWhereLike2(" ac.CUSTOMER_NAME", reqBean.getInfo("customerName"));
        }else {
        	 sqlBuilder.addWhereLike2(" ac.CUSTOMER_NAME", reqBean.getInfo("quickSearchName"));
        }
        sqlBuilder.appendSql(" order by abd.CREATE_TIME desc");
        return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    
    /**
     * 查询客户保证金信息
     * @param reqBean
     * @return
     */
    
    public JSONObject getCustomerDeposit(ReqBean reqBean){
    	JSONObject result = new JSONObject();
    	String bidId = reqBean.getInfo("bidId");
    	String customerId = reqBean.getInfo("customerId");
    	Customer customer = customerMapper.selectByPrimaryKey(customerId);
    	String sql = "select  *  from AD_BIDDING_DEPOSIT  abd  where abd.BID_ID ='"+bidId+"' and abd.CUSTOMER_ID='"+customerId+"'";
    	JSONArray dataArray = this.query(sql);
    	result.put("dataArray", dataArray);
    	result.put("customerName", customer.getCustomerName());
    	return result;
    }
    
    
    
    /**
     * 新增
     *
     * @param reqBean
     */
    public void addBiddingDeposit(ReqBean reqBean) {
        BiddingDeposit biddingDeposit = new BiddingDeposit();  
        biddingDeposit.setId(intIncrementId("AD_BIDDING_DEPOSIT", "ID"));// 手动设置Id
        biddingDeposit.setBidId(reqBean.getInfo("bidId"));
        biddingDeposit.setCustomerId(reqBean.getInfo("customerId"));
        biddingDeposit.setDeposit(reqBean.getInfoDouble("deposit"));
        biddingDeposit.setDepositSource(Constant.DEPOSIT_SOURCE_MANUAL);// 手动录入
        biddingDeposit.setOperator(reqBean.getUserId());
        biddingDeposit.setCreateTime(new Date());
        biddingDepositMapper.insert(biddingDeposit);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateBiddingDeposit(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        BiddingDeposit biddingDeposit = new BiddingDeposit();  
        biddingDeposit.setId(reqBean.getInfo("id"));// 手动设置Id
        biddingDeposit.setBidId(reqBean.getInfo("bidId"));
        biddingDeposit.setCustomerId(reqBean.getInfo("customerId"));
        biddingDeposit.setDeposit(reqBean.getInfoDouble("deposit"));
        biddingDeposit.setDepositSource(Constant.DEPOSIT_SOURCE_MANUAL);// 手动录入
        biddingDeposit.setOperator(reqBean.getUserId());
        biddingDeposit.setCreateTime(new Date());
        
        biddingDepositMapper.updateByPrimaryKeySelective(biddingDeposit);
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void removeBiddingDeposit(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        biddingDepositMapper.deleteByPrimaryKey(id);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private BiddingDeposit getBiddingDeposit(ReqBean reqBean) {
        BiddingDeposit biddingDeposit = new BiddingDeposit();

        biddingDeposit.setId(reqBean.getInfo("id"));
        biddingDeposit.setBidId(reqBean.getInfo("bidId"));
        biddingDeposit.setCustomerId(reqBean.getInfo("customerId"));
        biddingDeposit.setDeposit(reqBean.getInfoDouble("deposit"));
        biddingDeposit.setCreateTime(DateUtil.parseDateTime(reqBean.getInfo("createTime")));

        return biddingDeposit;
    }
    
    /**
     * 获得客户信息列表
     * @return
     */
    public JSONArray queryCustomerList(){
    	 String sql = "SELECT a.CUSTOMER_ID,a.CUSTOMER_NAME,a.STATUS FROM AD_CUSTOMER a where a.STATUS = '1'";
    	 return this.query(sql);
    }
    
    /**
     * 获得竞价单信息列表
     * @return
     */
    public JSONArray queryBidList(){
    	 String sql = "SELECT a.BID_ID,a.BID_TITLE,a.STATUS FROM AD_BIDDING a where a.STATUS = '1'";
    	 return this.query(sql);
    }

    /**
	 * 客户交保证金
	 * @param reqBean
	 */
	public void payDeposit(ReqBean reqBean) {
		String customerId = reqBean.getInfo("customerId");
		BiddingDeposit biddingDeposit = new BiddingDeposit();
		biddingDeposit.setId(intIncrementId("AD_BIDDING_DEPOSIT", "ID"));// 手动设置Id
		biddingDeposit.setBidId(reqBean.getInfo("bidId"));
		biddingDeposit.setCustomerId(customerId);
		biddingDeposit.setDeposit(reqBean.getInfoDouble("deposit"));
		biddingDeposit.setDepositSource(Constant.DEPOSIT_SOURCE_RECORD);// 缴费记录
		biddingDeposit.setOperator(reqBean.getInfo("customerId"));// 客户id
		biddingDeposit.setCreateTime(new Date());
		biddingDepositMapper.insert(biddingDeposit);
	}
    
	/**
	 * 客户交保证金
	 * @param reqBean
	 */
	public void payDeposit(String customerId, String bidId) {
		//先插入是否已经添加
		BiddingDeposit bd = biddingDepositMapper.selectByBidIdAndCustomerId(bidId, customerId);
		if(bd==null){// 客户保证金记录不存在
			Bidding bid = biddingMapper.selectByPrimaryKey(bidId);
			BiddingDeposit biddingDeposit = new BiddingDeposit();
			biddingDeposit.setId(intIncrementId("AD_BIDDING_DEPOSIT", "ID"));// 手动设置Id
			biddingDeposit.setBidId(bidId);
			biddingDeposit.setCustomerId(customerId);
			biddingDeposit.setDeposit(bid.getDeposit());
			biddingDeposit.setDepositSource(Constant.DEPOSIT_SOURCE_RECORD);// 缴费记录
			biddingDeposit.setOperator(customerId);// 客户id
			biddingDeposit.setCreateTime(new Date());
			biddingDepositMapper.insert(biddingDeposit);
		}else {
			LogUtil.writeLog("客户已经提交过保证金!");
		}
	}
	
    
}